﻿/*
COPYRIGHT (C) 2008 EPISERVER AB

THIS FILE IS PART OF SCRUM DASHBOARD.

SCRUM DASHBOARD IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUM DASHBOARD IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUM DASHBOARD. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Linq;
using System.Runtime.Serialization;
using ScrumDashboard.Library;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Script.Services;
using ScrumDashboard.Library.Tfs;
using ScrumDashboard.Library.Model;

namespace ScrumDashboard
{
    [ScriptService]
    public class AjaxService : System.Web.Services.WebService
    {
        [WebMethod]
        public bool IsLatestVersion(int workItemID, int workItemRevisionID)
        {
            TfsManager tfs = new TfsManager();
            return tfs.GetWorkItem(workItemID).Revision==workItemRevisionID;
        }

        [WebMethod]
        public void MoveToState(int workItemID, string state)
        {
            TfsManager tfs = new TfsManager();
            string tfsState;

            switch (state)
            {
                case "NotDone":
                    tfsState = ConchangoFields.State_NotDone;
                    break;
                case "InProgress":
                    tfsState = ConchangoFields.State_InProgress;
                    break;
                case "ReadyForTest":
                    tfsState = ConchangoFields.State_ReadyForTest;
                    break;
                case "Done":
                    tfsState = ConchangoFields.State_Done;
                    break;
                default:
                    throw new Exception("Invalid state " + state);

            }
            tfs.MoveWorkItemToState(workItemID, tfsState);
        }

        [WebMethod]
        public User AssignTo(int workItemID, string state, string assignedTo)
        {
            TfsManager tfs = new TfsManager();
            string tfsState;

            try
            {
                switch (state)
                {
                    case "NotDone":
                        tfsState = ConchangoFields.State_NotDone;
                        break;
                    case "InProgress":
                        tfsState = ConchangoFields.State_InProgress;
                        break;
                    case "ReadyForTest":
                        tfsState = ConchangoFields.State_ReadyForTest;
                        break;
                    case "Done":
                        tfsState = ConchangoFields.State_Done;
                        break;
                    default:
                        throw new Exception("Invalid state " + state);
                }

                return tfs.MoveWorkItemToState(workItemID, tfsState, assignedTo);
            }
            catch(Exception ex)
            {
                throw new Exception("Problem with assign to a person " + ex.Message);
            }
        }

        [WebMethod]
        public void ChangeTime(int itemID, float time)
        {
            TfsManager tfs = new TfsManager();

            try
            {
                tfs.ChangeWorkRemaining(itemID, time);
            }
            catch (Exception)
            {
                throw new Exception("Problem with set time for the item");
            }
        }
        // Add more operations here and mark them with [OperationContract]
    }
}
